package com.hotel.room.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotel.common.param.UserRoomRate;
import com.hotel.common.vo.R;
import com.hotel.user.entity.HotelRoomRate;
import com.hotel.room.mapper.HotelRoomRateMapper;
import com.hotel.room.service.HotelRoomRateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author trc
 * @since 2024-06-22
 */
@Service
public class HotelRoomRateServiceImpl extends ServiceImpl<HotelRoomRateMapper, HotelRoomRate> implements HotelRoomRateService {

    @Autowired
    private HotelRoomRateMapper hotelRoomRateMapper;

    @Override
    public R showUserRoomRate(Integer userId, UserRoomRate userRoomRate) {
        Integer roomId = userRoomRate.getRoomId();
        HotelRoomRate hotelRoomRate = hotelRoomRateMapper.selectRateListById(userId, roomId);
        if (hotelRoomRate != null) {
            return R.ok(hotelRoomRate.getRate());
        }
        return R.ok(0);
    }

    @Override
    public R changeUserRoomRate(Integer userId, UserRoomRate userRoomRate) {
        Integer roomId = userRoomRate.getRoomId();
        HotelRoomRate hotelRoomRate = hotelRoomRateMapper.selectRateListById(userId, roomId);
        if (hotelRoomRate == null) {
            // 添加用户对该房间的评分
            hotelRoomRate = new HotelRoomRate();
            hotelRoomRate.setUserId(userId);
            hotelRoomRate.setRoomId(roomId);
            hotelRoomRate.setRate(userRoomRate.getRate());
            hotelRoomRate.setCtime(new Date());

            int num = hotelRoomRateMapper.insert(hotelRoomRate);
            if (num > 0) {
                return R.ok("修改成功");
            }
        } else {
            // 修改用户对该房间的评分
            hotelRoomRate.setRate(userRoomRate.getRate());

            int num = hotelRoomRateMapper.updateById(hotelRoomRate);
            if (num > 0) {
                return R.ok("修改成功");
            }
        }
        return R.fail("修改失败");
    }
}
